package com.java.tutorials.jdk7.asynchronouschannel;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Future;
/**
 * Created by xuzhike on 2016/1/20.
 * http://www.java2s.com/Code/Java/JDK-7/UsingAsynchronousFileChannelandFuturetoread.htm
 * Using AsynchronousFileChannel and Future to read
 */
public class AsynchronousFileChannelTest {
    public static final String FILE="E:\\OpenSources\\MyProjects\\javaTutorials\\src\\main\\java\\com\\java\\tutorials\\jdk7\\asynchronouschannel\\test.txt";
    public static void main(String[] args) throws Exception {
        Path file = Paths.get(FILE);
        AsynchronousFileChannel channel = AsynchronousFileChannel.open(file);

        ByteBuffer buffer = ByteBuffer.allocate(100_000);
        Future<Integer> result = channel.read(buffer, 0);

        while (!result.isDone()) {
            ProfitCalculator.calculateTax();
        }
        Integer bytesRead = result.get();
        System.out.println("Bytes read [" + bytesRead + "]");
    }
}

class ProfitCalculator {
    public ProfitCalculator() {
    }
    public static void calculateTax() {
    }
}
